package com.xhwl.data.controller;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.sdata.AppVersionDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.AppVersion;
import com.xhwl.common.query.cent.sdata.AppVersionQuery;
import com.xhwl.common.vo.cent.sdata.AppVersionVO;
import com.xhwl.data.service.IAppService;
import com.xhwl.data.service.IAppVersionService;
import com.xhwl.data.util.VersionUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author JCccc
 * @Description
 * @Date 2021/7/17 10:48
 */
@RestController
public class AppVersionController {
    private static final Logger log = LoggerFactory.getLogger(AppVersionController.class);
    @Autowired
    IAppVersionService appVersionService;
    @Autowired
    IAppService appService;

    /**
     * 创建
     *
     * @return
     */
    @PostMapping(value = "/version/create")
    public ResultJson create(
            @RequestHeader(value = "appId", required = true) String appId,
            @RequestHeader(value = "clientType", required = false) Integer clientType,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestBody AppVersionDTO appVersionDTO) {
        String appIdStr = appVersionDTO.getAppId();
        App app = appService.getByAppId(appIdStr);
        if (null == app) {
            log.info("当前数据库无此appId={}相关数据，请核实",appIdStr);
            return ResultJson.fail("app参数异常");
        }
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(appVersionDTO, appVersion);
        appVersion.setAppId(app.getId());
        if (StringUtils.isEmpty(appVersion.getPrevVersion())) {
            appVersion.setPrevVersion(appVersion.getNewVersion());
        }
        return appVersionService.create(appVersion);
    }

    /**
     * 更新
     *
     * @param appId
     * @param appVersionDTO
     * @return
     */
    @PostMapping(value = "/version/update")
    public ResultJson update(
            @RequestHeader(value = "appId", required = true) String appId,
            @RequestHeader(value = "clientType", required = false) Integer clientType,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestBody AppVersionDTO appVersionDTO) {
        String appIdStr = appVersionDTO.getAppId();
        App app = appService.getByAppId(appIdStr);
        if (null == app) {
            log.info("当前数据库无此appId={}相关数据，请核实",appIdStr);
            return ResultJson.fail("app参数异常");
        }
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(appVersionDTO, appVersion);
        appVersion.setAppId(app.getId());
        return appVersionService.update(appVersion);
    }

    /**
     * 删除
     *
     * @param appId
     * @param id
     * @return
     */
    @GetMapping(value = "/version/delete")
    public ResultJson delete(
            @RequestHeader(value = "appId", required = true) String appId,
            @RequestHeader(value = "clientType", required = false) Integer clientType,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestParam Integer id) {
        return appVersionService.delete(id);
    }

    /**
     * 获取 分页
     *
     * @param appId
     * @return
     */
    @GetMapping(value = "/version/page-list")
    public ResultJson pageList(
            Page page,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestParam(value = "likeName", required = false) String likeName,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "clientType", required = false) Integer clientType) {

        AppVersionQuery appVersionQuery = new AppVersionQuery();
        if (StringUtils.isNotEmpty(appId)) {
            appVersionQuery.setAppId(appId);
        }
        if (StringUtils.isNotEmpty(likeName)) {
            appVersionQuery.setLikeName(likeName);
        }
        if (null != clientType) {
            appVersionQuery.setClientType(clientType);
        }
        Page<AppVersionDTO> appVersionDTOPage = appVersionService.pageList(appVersionQuery, page);
        return ResultJson.success(appVersionDTOPage);
    }

    /**
     * 获取 不分页
     *
     * @param appId
     * @return
     */
    @GetMapping(value = "/version/list")
    public ResultJson list(
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestParam(value = "likeName", required = false) String likeName,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "clientType", required = false) Integer clientType) {
        AppVersionQuery appVersionQuery = new AppVersionQuery();
        if (StringUtils.isNotEmpty(appId)) {
            appVersionQuery.setAppId(appId);
        }
        if (StringUtils.isNotEmpty(likeName)) {
            appVersionQuery.setLikeName(likeName);
        }
        if (null != clientType) {
            appVersionQuery.setClientType(clientType);
        }
        List<AppVersionDTO> list = appVersionService.list(appVersionQuery);
        return ResultJson.success(list);
    }

    /**
     * 关于版本
     *
     * @return
     */
    @GetMapping(value = "/version/about")
    public ResultJson aboutVersion(@RequestHeader(value = "appId", required = true) String appId,
                                   @RequestHeader(value = "clientType", required = false) Integer clientType,
                                   @RequestHeader(value = "industryId", required = true) Short industryId,
                                   @RequestParam("currentVersion") String currentVersion) {
        App app = appService.getByAppId(appId);
        if (null == app) {
            log.info("当前数据库无此appId={}相关数据，请核实",appId);
            return ResultJson.fail("app参数异常");
        }
        Integer appKeyId = app.getId();
        AppVersionQuery appVersionQuery = new AppVersionQuery();
        appVersionQuery.setAppKeyId(appKeyId);
        appVersionQuery.setClientType(clientType);
        AppVersion newestVersion = appVersionService.getNewestVersion(appVersionQuery);
        if (null == newestVersion) {
            return ResultJson.success("暂无版本信息");
        }
        String newVersion = newestVersion.getNewVersion();
        Boolean haveNewVersion = VersionUtil.compareVersion(newVersion, currentVersion);
        AppVersionVO appVersionVO = new AppVersionVO();
        BeanUtils.copyProperties(newestVersion, appVersionVO);
        //额外补充一些信息
        appVersionVO.setAppName(app.getName());
        appVersionVO.setHaveNewVersion(haveNewVersion);
        return ResultJson.success(appVersionVO);
    }

}
